﻿using System;
using UnityEngine;

public enum NodeState { Blank, Wall, Snake, Food }
[RequireComponent(typeof(SpriteRenderer))]
public class Node : MonoBehaviour
{
    public int X { get; private set; }
    public int Y { get; private set; }
    private NodeState state;
    private SpriteRenderer spriteRenderer;
    private Sprite blankSprite;
    private Sprite wallSprite;
    private Sprite foodSprite;
    private Sprite snakeSprite;
    private SnakeDirection nodeDirection;
    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        wallSprite = Resources.Load<Sprite>("Sprites/Wall");
        blankSprite = Resources.Load<Sprite>("Sprites/Wall");
        foodSprite = Resources.Load<Sprite>("Sprites/Food");
        snakeSprite = Resources.Load<Sprite>("Sprites/Snake");
    }
    public void SetPosition(int x, int y)
    {
        this.X = x;
        this.Y = y;
        transform.position = new Vector2(x, y);
    }
    public Vector2 GetPosition() => new Vector2(X, Y);
    public NodeState GetState() => state;
    public void SetState(NodeState state)
    {
        this.state = state;
        switch (state)
        {
            case NodeState.Blank:
                if (spriteRenderer.sprite != blankSprite)
                {
                    spriteRenderer.sprite = blankSprite;
                    spriteRenderer.color = Color.gray;
                }
                break;
            case NodeState.Wall:
                if (spriteRenderer.sprite != wallSprite)
                {
                    spriteRenderer.sprite = wallSprite;
                    spriteRenderer.color = Color.black;
                }
                break;
            case NodeState.Snake:
                if (spriteRenderer.sprite != snakeSprite)
                {
                    spriteRenderer.sprite = snakeSprite;
                    spriteRenderer.color = Color.cyan;
                }
                break;
            case NodeState.Food:
                if (spriteRenderer.sprite != foodSprite)
                {
                    spriteRenderer.sprite = foodSprite;
                    spriteRenderer.color = Color.yellow;
                }
                break;
        }
    }
    public void SetRotation()
    {
        switch (nodeDirection)
        {
            case SnakeDirection.Left:
                transform.rotation = Quaternion.Euler(0, 0, 90f);
                break;
            case SnakeDirection.Right:
                transform.rotation = Quaternion.Euler(0, 0, -90f);
                break;
            case SnakeDirection.Up:
                transform.rotation = Quaternion.Euler(0, 0, 0);
                break;
            case SnakeDirection.Down:
                transform.rotation = Quaternion.Euler(0, 0, 180f);
                break;
        }
    }
    public void SetDirection(SnakeDirection direction)
    {
        switch (direction)
        {
            case SnakeDirection.Left:
                nodeDirection = SnakeDirection.Left;
                break;
            case SnakeDirection.Right:
                nodeDirection = SnakeDirection.Right;
                break;
            case SnakeDirection.Up:
                nodeDirection = SnakeDirection.Up;
                break;
            case SnakeDirection.Down:
                nodeDirection = SnakeDirection.Down;
                break;
        }
    }
    public SnakeDirection GetDirection()
    {
        return nodeDirection;
    }
    public void ClearRotation()
    {
        transform.rotation = Quaternion.Euler(0, 0, 90f);
    }
}